import requests
import smtplib
import xlrd
from email.mime.text import MIMEText
from email.header import Header

# 打开文件
workBook = xlrd.open_workbook('./excel/打卡名单.xlsx')
# 按sheet名字获取sheet内容
sheet1_content = workBook.sheet_by_name('User')
# 打卡人数=行数-1
sum = sheet1_content.nrows-1
temp = 1

url1 = "http://xxcj.scnucas.com/xxcj/Login_ck.php"# 登录接口
url2 = "http://xxcj.scnucas.com/xxcj/fx_action.php"# 打卡接口

for i in range(sum):
    snum = sheet1_content.cell_value(temp, 1)#获取学号
    name = sheet1_content.cell_value(temp, 2)#获取姓名
    placecode = sheet1_content.cell_value(temp, 3)#获取打卡地点行政代码
    place = sheet1_content.cell_value(temp, 4)#获取打卡地点
    email = sheet1_content.cell_value(temp, 5)#获取邮箱

    print('正在执行：'+ str(temp))
    # form-data参数要写成如下格式，注意有None
    data0 = {
        "xh": (None, snum),
        "xm": (None, name)
    }
    data1 = {
        "post_date": (None, "day_tj1=A&day_tj2=A&day_tj2_zzxq=&day_tj4=A&day_tj4_1=&day_tj5=B&day_tj7=0&szdq_no=" + placecode[0:2] + "," + placecode[0:4] + ","+placecode+"&szdq_vl=" + place + "&jianyi=&lx=1")
    }
    data2 = {
        "post_date": (None, "day_tj1=A&day_tj2=A&day_tj2_zzxq=&day_tj4=A&day_tj4_1=&day_tj5=B&day_tj7=0&szdq_no=" + placecode[0:2] + "," + placecode[0:4] + ","+placecode+"&szdq_vl=" + place + "&jianyi=&lx=2")
    }
    data3 = {
        "post_date": (None, "day_tj1=A&day_tj2=A&day_tj2_zzxq=&day_tj4=A&day_tj4_1=&day_tj5=B&day_tj7=0&szdq_no=" + placecode[0:2] + "," + placecode[0:4] + ","+placecode+"&szdq_vl=" + place + "&jianyi=&lx=3")
    }
    #构造Session会话
    session = requests.Session()
    #在session中发送登录请求，此后这个session里就存储了cookie
    session.post(url1, files=data0) #模拟登陆
    #提交健康打卡
    #早
    responsedk1 = session.post(url2,data=data1)
    back1 = responsedk1.content.decode('utf-8')
    back1 = back1.replace('\r','').replace('\n','').replace('\t','')
    key1 = '{"code":0'
    key2 = '{"code":1'
    if key1 in back1:
      print(name+'早上段填报成功!')
    if key2 in back1:
      print(name+'今天早上段已经提交!')
    #中
    responsedk2 = session.post(url2,data=data2)
    back2 = responsedk2.content.decode('utf-8')
    back2 = back2.replace('\r','').replace('\n','').replace('\t','')
    if key1 in back2:
      print(name+'中午段填报成功!')
    if key2 in back2:
      print(name+'今天中午段已经提交!')
    #晚
    responsedk3 = session.post(url2,data=data3)
    back3 = responsedk3.content.decode('utf-8')
    back3 = back3.replace('\r','').replace('\n','').replace('\t','')
    if key1 in back3:
      print(name+'晚上段填报成功!')
    if key2 in back3:
      print(name+'今天晚上段已经提交!')
    session.cookies.clear()
    session.close()

    temp += 1

    # 第三方 SMTP 服务
    mail_host="smtp.qq.com"  #设置服务器
    mail_user="944592024@qq.com"    #用户名
    mail_pass=""   #口令

    sender = '944592024@qq.com'
    subject = '健康打卡成功提醒'

    tips = name+'您好！每日健康打卡成功！\n'+'打卡地点：' + place + '\n\n'+'健康打卡，是对自己的健康负责，更是对学校负责，希望大家担负起自己的社会责任，积极打卡，从我做起。'
    receivers = [email]  # 接收邮件
    message = MIMEText(tips, 'plain', 'utf-8') #邮件正文
    message['From'] = Header("健康文理打卡系统", 'utf-8') #发送方
    message['To'] =  Header(name, 'utf-8') #接收方
    message['Subject'] = Header(subject, 'utf-8')

    if key1 in back1:
        if key1 in back2:
            if key1 in back3:
              try:
                  smtpObj = smtplib.SMTP()
                  smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
                  smtpObj.login(mail_user,mail_pass)
                  smtpObj.sendmail(sender, receivers, message.as_string())
                  print ("向"+name+"发送邮件成功")
              except smtplib.SMTPException:
                  print ("Error: 无法向"+name+"发送邮件")

